package com.usooft.metaTalent.admin.system.form;

import com.usooft.framework.web.form.ListForm;
import com.usooft.metaTalent.core.system.query.EmployeeQuery;
import lombok.Setter;

import java.time.LocalDateTime;

@Setter
public class EmployeeListForm extends ListForm<EmployeeQuery> {

    private LocalDateTime createdDateOnOrBefore;
    private LocalDateTime createdDateOnOrAfter;
    private String phoneNumberContains;
    private String genderIs;
    private Boolean lockedIs;
    private String nameContains;

    @Override
    public EmployeeQuery buildQuery() {
        return EmployeeQuery.builder()
                .filter(EmployeeQuery.Filter.builder()
                        .createdDateOnOrBefore(createdDateOnOrBefore)
                        .createdDateOnOrAfter(createdDateOnOrAfter)
                        .phoneNumberContains(phoneNumberContains)
                        .nameContains(nameContains)
                        .genderIs(genderIs)
                        .lockedIs(lockedIs)
                        .build())
                .sorts(buildSorts())
                .wraps(EmployeeQuery.Wraps.builder()
                        .build())
                .limit(getLimit())
                .build();
    }

    private EmployeeQuery.Sorts buildSorts() {
        EmployeeQuery.Sorts.Builder builder = EmployeeQuery.Sorts.builder();
        for (String sortItem : getSortItems()) {
            switch (sortItem) {
                case "createdDate", "-createdDate" -> builder.id(getSortOrder(sortItem));
                default -> {
                }
            }
        }
        return builder.build();
    }

}
